package com.jh.complicate.serializable;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author Administrator
 * @projectName newforward
 * @title HessianSerializer.java
 * @package com.jh.complicate.serializable
 * @description Hession序列化
 * @date 2019/6/5 16:07
 */
public class HessianSerializer implements ISerializable{
    @Override
    public <T> byte[] serialize(T obj) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(bos);
        try {
            output.writeObject(obj);
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public <T> T deserialize(byte[] data, Class clzz) {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        HessianInput input = new HessianInput(bis);
        try {
            return (T) input.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
